<?php
global $_W, $_GPC;
$weid = $this->_weid;
$from_user = $this->_fromuser;
$setting = $this->getSetting();

if ($setting['auth_mode'] == 1 || empty($setting)) {
    $method = 'marketingstore'; //method
    $host = $this->getOAuthHost();
    $authurl = $host . 'app/' . $this->createMobileUrl($method, array(), true) . '&authkey=1';
    $url = $host . 'app/' . $this->createMobileUrl($method, array(), true);
    if (isset($_COOKIE[$this->_auth2_openid])) {
        $from_user = $_COOKIE[$this->_auth2_openid];
        $nickname = $_COOKIE[$this->_auth2_nickname];
        $headimgurl = $_COOKIE[$this->_auth2_headimgurl];
    } else {
        if (isset($_GPC['code'])) {
            $userinfo = $this->oauth2($authurl);
            if (!empty($userinfo)) {
                $from_user = $userinfo["openid"];
                $nickname = $userinfo["nickname"];
                $headimgurl = $userinfo["headimgurl"];
            } else {
                message('授权失败!');
            }
        } else {
            if (!empty($this->_appsecret)) {
                $this->getCode($url);
            }
        }
    }
} else {
    load()->model('mc');
    if (empty($_W['fans']['nickname'])) {
        mc_oauth_userinfo();
    }
    $from_user = $_W['fans']['openid'];
    $nickname = $_W['fans']['nickname'];
    $headimgurl = $_W['fans']['tag']['avatar'];
}

//市场专员
$marketing = pdo_fetch("SELECT * FROM " . tablename('weisrc_dish_marketing') . " WHERE weid = :weid AND from_user=:from_user LIMIT 1;", array(':weid' => $this->_weid, ':from_user' => $from_user));
if (empty($marketing)) {
    message('您不是市场专员，无权访问！');
}
$op = $_GPC['op'];



$fans = $this->getFansByOpenid($from_user);
if (empty($fans)) {
    $this->addFans($nickname, $headimgurl);
} else {
    $this->updateFans($nickname, $headimgurl, $fans['id']);
}
$fans = $this->getFansByOpenid($from_user);
if ($fans['status'] == 0) {
    die('系统调试中！');
}
if (empty($from_user)) {
    message('会话已过期，请重新发送关键字!');
}

//if (!empty($_GPC['time'])) {
//    $starttime = strtotime($_GPC['time']);
//    $selecttime = $_GPC['time'];
//} else {
//    $starttime = TIMESTAMP;
//    $selecttime = date('Y-m-d');
//}
$selecttime = date('Y-m-d');

$loglist = pdo_fetchall("SELECT * FROM " . tablename('weisrc_dish_marketinghandlelog') . " WHERE weid = :weid AND type=1 and  date = :date  ", array(':weid' => $weid, ':date' => $selecttime), 'logid');

$where = " WHERE weid ={$weid} AND is_show=1 AND marketingid={$marketing['id']}";
$list = pdo_fetchall("SELECT * FROM " . tablename($this->table_stores) . " {$where} order by displayorder desc,id desc ");

$condition = '';
$zero_time = mktime(0, 0, 0);
if ($op == 'today') {
    $title = '今日订单';
    $condition = "  AND dateline>{$zero_time} ";
} else if ($op == 'nowmonth') {
    $title = '本月订单';
    $condition = " AND DATE_FORMAT(FROM_UNIXTIME(dateline), '%Y%m' )= DATE_FORMAT(CURDATE(), '%Y%m') ";
} else if ($op == 'lastmonth') {
    $title = '上月订单';
    $condition = " AND PERIOD_DIFF(date_format(now(),'%Y%m'), date_format(FROM_UNIXTIME(dateline), '%Y%m')) =1 ";
} else if ($op == 'shop') {
    $title = '门店管理';
    $condition = "  AND dateline>{$zero_time} ";
}

foreach ($list as $key => $value) {
    //日交易总金额
    $totalprice = pdo_fetchcolumn("SELECT sum(totalprice) FROM " . tablename($this->table_order) . "
WHERE weid=:weid AND storeid=:storeid AND status=3 AND (paytype=1 OR paytype=2 OR paytype=4) AND ispay=1 AND
ismerge=0 {$condition} ", array(':weid' => $this->_weid, ':storeid' => $value['id']));
    $totalprice = sprintf('%.3f', $totalprice);
    $list[$key]['totalprice'] = $totalprice;

    $marketingprice = pdo_fetchcolumn("SELECT sum(marketingprice)  FROM " . tablename($this->table_order) . "
WHERE weid=:weid AND storeid=:storeid AND status=3 AND (paytype=1 OR paytype=2 OR paytype=4) AND ispay=1 AND ismerge=0 {$condition} ", array(':weid' => $this->_weid, ':storeid' => $value['id']));

    $marketingprice = sprintf('%.3f', $marketingprice);
    $list[$key]['marketingprice'] = $marketingprice;
}

include $this->template($this->cur_tpl . '/marketingstore');